<template>
  <div class="adoptedIndex">
    <search-block>
      <template slot="button">
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="addIndex" v-if="isEdit">添加指标</el-button>
      </template>
      <el-form inline :model="model" @submit.native.prevent>
        <el-form-item label="指标种类:">
          <y-code-select
            v-model="model.indexType"
            clearable
            dict="adopted_index_type"></y-code-select>
        </el-form-item>
      </el-form>
    </search-block>
    <y-table
        ref="table"
        border
        pagination
        :getDataWay="getDataWay"
        :columns="columns"
    >
      <el-table-column slot="y-oper" label="操作" align="center" header-align="center" width="150" v-if="isEdit">
        <template v-slot="scope">
          <div class="table-buttons">
            <el-button type="primary" size="mini" @click="editIndex(scope.row)">编辑</el-button>
            <el-popconfirm
                title="是否确认删除该指标？"
                @confirm="delIndex(scope.row)">
              <el-button slot="reference" type="danger" size="mini">删除</el-button>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </y-table>
    <y-form
        v-model="forms.formModel"
        :visible.sync="forms.showForm"
        :title="forms.formTypeTitle[forms.formsType]"
        destroy-on-close
        append-to-body
        width="50%"
        :form-attrs="forms.formAttrs"
        :columns="forms.formColumns"
        @ensure="ensureModel">
    </y-form>
  </div>
</template>

<script>
export default {
  name: "adoptedIndex",
  inject: ['getDictNameSync', 'pattern'],
  props:{
    adoptedId: String,
    isEdit: Boolean
  },
  computed:{
    getDataWay(){
      return{
        dataType: 'url',
        data: this.$api.adopted.queryAdoptedIndexList,
        param: Object.assign({
          adoptedId: this.adoptedId
        }, this.model)
      }
    }
  },
  data(){
    return {
      model:{
        indexType: '',
      },
      columns: [{
        label: "指标类型",
        prop: "indexType",
        formatter: (function(row){
          return this.getDictNameSync(row.indexType, 'adopted_index_type');
        }).bind(this)
      },{
        label: "指标值",
        prop: "indexValue"
      },{
        label: "指标日期",
        prop: "indexDate"
      },{
        label: "操作人",
        prop: "updateUserName"
      }],
      forms:{
        formsType: '',
        formTypeTitle: {
          'ADD': '添加指标',
          'EDIT': '编辑指标'
        },
        formModel:{
          indexType: '',
          indexValue: '',
          indexDate: ''
        },
        currentForm: null,
        showForm: false,
        formAttrs:{
          labelWidth: '100px',
          rules: {
            indexType: [{required: true, message: "请选择指标类型", trigger: "change"}],
            indexValue: [
              {required: true, message: "请输入指标值", trigger: "blur"}
            ],
            indexDate: [{required: true, message: "请选择指标日期", trigger: "change"}]
          }
        },
        formColumns:[
          {
            label: "指标类型",
            prop: "indexType",
            comp:{
              compType: 'YCodeSelect',
              attrs: {
                dict: 'adopted_index_type'
              }
            }
          },
          {
            label: "指标值",
            prop: "indexValue",
            comp:{
              compType: 'input-number',
              attrs:{
                stepStrictly: true,
                min: 0,
                step: 0.01,
                controls: false
              }
            }
          },{
            label: "指标日期",
            prop: "indexDate",
            comp:{
              compType: 'datePicker',
              attrs: {
                type: "date",
                pickerOptions:{
                  disabledDate(time) {
                    return time.getTime() > Date.now();
                  },
                  shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                      picker.$emit('pick', new Date());
                    }
                  }]
                },
                valueFormat: 'yyyy-MM-dd'
              }
            }
          }
        ],
      }
    }
  },
  methods:{
    resetForm(){
      Object.keys(this.forms.formModel).map(key => {
        this.forms.formModel[key] = this.forms.currentForm ? this.forms.currentForm[key] : '';
      })
    },
    addIndex(){
      this.forms.formsType = 'ADD';
      this.forms.currentForm = null;
      this.resetForm();
      this.forms.showForm = true;
    },
    search(){
      this.$refs['table'].loadData(Object.assign({
        adoptedId: this.adoptedId
      }, this.model));
    },
    editIndex(row){
      this.forms.formsType = 'EDIT';
      this.forms.currentForm = row;
      this.resetForm();
      this.forms.showForm = true;
    },
    delIndex(row){
      this.$yAxios({
        url: this.$api.adopted.removeAdoptedIndex,
        data:{
          ids: [row.id]
        }
      }, info => {
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.search();
      })
    },
    ensureModel(form){
      let newForm;
      if(this.forms.formsType === 'EDIT'){
        newForm = Object.assign({}, this.forms.currentForm, form);
      }else{
        newForm = Object.assign({ adoptedId: this.adoptedId }, form);
      }
      this.$yAxios({
        url: this.$api.adopted.saveAdoptedIndex,
        data: newForm
      }, info => {
        this.$message({
          type: 'success',
          message: '保存成功'
        });
        this.search();
      })
    },
  }
}
</script>

<style scoped>

</style>